//
//  RBPwdItem.m
//  4-24-1 手势密码demo
//
//  Created by 华杨科技 on 2017/4/24.
//  Copyright © 2017年 饶波. All rights reserved.
//
//  手势密码的密码：圆点

#import "RBPwdItem.h"
#import "RBPwdHeader.h"
@implementation RBPwdItem


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    // 不显示

    
    // 懒加载一定要用self.点调用
    [self.layer addSublayer:self.outterLayer];
    [self.layer addSublayer:self.innerLayer];
    [self.layer addSublayer:self.triangleLayer];
 
    self.triangleLayer.hidden = YES;
}

#pragma mark setter方法/懒加载子视图
- (CAShapeLayer *)outterLayer {
    if (!_outterLayer) {
        _outterLayer = [[CAShapeLayer alloc] init];
        _outterLayer.frame = CGRectMake((self.frame.size.width - ITEMRADIUS_OUTTER)/2, (self.frame.size.width - ITEMRADIUS_OUTTER)/2, ITEMRADIUS_OUTTER, ITEMRADIUS_OUTTER);
        _outterLayer.fillColor = BACKGROUNDCOLOR.CGColor;
        _outterLayer.strokeColor = [UIColor whiteColor].CGColor; // 填充路径颜色:白色
        _outterLayer.lineWidth = ITEMRADIUS_LINEWIDTH; // 线条宽度
        
        //贝塞尔
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:_outterLayer.bounds];
        _outterLayer.path = bezierPath.CGPath;
    }
    return _outterLayer;
}

- (CAShapeLayer *)innerLayer {
    if (!_innerLayer) {
        _innerLayer = [[CAShapeLayer alloc] init];
        _innerLayer.frame = CGRectMake((self.frame.size.width-ITEMRADIUS_INNER)/2, (self.frame.size.width-ITEMRADIUS_INNER)/2, ITEMRADIUS_INNER, ITEMRADIUS_INNER);
        
        _innerLayer.fillColor = [UIColor clearColor].CGColor;

        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:_innerLayer.bounds];
        _innerLayer.path = bezierPath.CGPath;
    }
    return _innerLayer;
}

- (CAShapeLayer *)triangleLayer {

    
    if (!_triangleLayer) {
        _triangleLayer = [CAShapeLayer layer];
        _triangleLayer.frame = CGRectZero;
        _triangleLayer.fillColor = SELECTCOLOR.CGColor;
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(self.frame.size.width/2, self.innerLayer.frame.origin.y - 10)];
        [path addLineToPoint:CGPointMake(self.frame.size.width/2 - 5, self.innerLayer.frame.origin.y-3)];
     
     // 被坑了，坐标写错了，死活不显示，对了n遍；最后通过代码对比工具，找出问题来了。
        [path addLineToPoint:CGPointMake(self.frame.size.width/2 + 5, self.innerLayer.frame.origin.y-3)];
        
        _triangleLayer.path = path.CGPath;
    }
    
    return _triangleLayer;
}


// styleModel setter 方法
- (void)setStyleModel:(SelectStyleModel)styleModel {
    switch (styleModel) {
        case wrongStyle:{
            [self wrongUI];
        }break;
            
        case selectStyle:{
            [self selectUI];
        }break;
            
        case normalStyle:{
            [self normalUI];
        }break;
            
        default:
            
            break;
    }
}


- (void)selectUI{
    self.innerLayer.fillColor = SELECTCOLOR.CGColor;
    self.outterLayer.strokeColor = SELECTCOLOR.CGColor;
}

- (void)normalUI {
    self.innerLayer.fillColor = [UIColor clearColor].CGColor; // 内园填充颜色
    self.outterLayer.strokeColor = [UIColor whiteColor].CGColor; // 外圆轮廓颜色
}
- (void)wrongUI{
    self.innerLayer.fillColor = WRONGCOLOR.CGColor;
    self.outterLayer.strokeColor = WRONGCOLOR.CGColor;
}

/**判断三角形箭头方向*/
- (void)judgeTriangleDirectionWithx1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 isHidden:(BOOL)isHidden{
    
    if (isHidden) {
        self.triangleLayer.hidden = YES;
        return;
    }
    if (x1==x2 && y1==y2) {
        return;
    }
    if (x1==0 && y1==0) {
        return;
    }
    if (x2==0 && y2==0) {
        return;
    }
    if (self.triangleLayer.hidden==NO) {
        return;
    }
    
    self.triangleLayer.hidden = NO;
    CGFloat angle ;
    
    if (x1<x2 && y1>y2) {
        // 左上
        angle = M_PI_4;
    }
    
    
    else if (x1<x2 && y1==y2) {
        // 左
        angle = M_PI_2;
    }
    
    
    else if (x1<x2 && y1<y2) {
        // 左下
        angle = M_PI_4 * 3;
    }
    
    
    else if (x1==x2 && y1<y2) {
        // 下
        angle = M_PI;
    }
    
    
    else if (x1>x2 && y1<y2) {
        // 右下
        angle = -M_PI_4 * 3;
    }
    
    
    else if (x1>x2 && y1==y2) {
        // 右
        angle = - M_PI_2;
    }
    
    
    else if (x1>x2 && y1>y2) {
        // 右上
        angle = - M_PI_4 *3 ;
    }
    
    else {
        angle = .0f;
    }
    
//    NSLog(@"..................... angle = [%f]", angle);
    self.transform = CGAffineTransformMakeRotation(angle);

}

@end
